﻿using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ToracTechnologies.ExpressionTreeLibrary.DynamicApi;

namespace ToracTechnologies.ExpressionTreeLibrary.ExtensionMethods.IEnumerable
{

    /// <summary>
    /// Extension Methods For IEnumerable
    /// </summary>
    public static class IEnumerableExtMethods
    {

        #region Any With Null Check

        /// <summary>
        /// Checks To See If The Collection Has Any Items (Same Idea As The Any Extension Method Of Off IEnumerable). Only Difference Is, If The Collection Is Null It Returns False Instead Of Throwing An Error
        /// </summary>
        /// <typeparam name="T">Type Of The IEnumerable</typeparam>
        /// <param name="Collection">Collection To Check Against</param>
        /// <returns>If The Item Has Any Records</returns>
        public static bool AnyWithNullCheck<T>(this IEnumerable<T> Collection)
        {
            //if it's null then return false
            if (Collection == null)
            {
                return false;
            }
            else
            {
                //otherwise use the extension method
                return Collection.Any();
            }
        }

        /// <summary>
        /// Checks To See If The Collection Has Any Items (Same Idea As The Any Extension Method Of Off IEnumerable). Only Difference Is, If The Collection Is Null It Returns False Instead Of Throwing An Error
        /// </summary>
        /// <typeparam name="T">Type Of The IEnumerable</typeparam>
        /// <param name="Collection">Collection To Check Against</param>
        /// <param name="Predicate">Predicate To Query The Collection With A Where Before We Determine If Any Exist</param>
        /// <returns>If The Item Has Any Records</returns>
        public static bool AnyWithNullCheck<T>(this IEnumerable<T> Collection, Func<T, bool> Predicate)
        {
            //if it's null then return false
            if (Collection == null)
            {
                return false;
            }
            else
            {
                //otherwise use the extension method
                return Collection.Any(Predicate);
            }
        }

        #endregion

        #region Paginate

        /// <summary>
        /// Takes A List Of Objects And Grabs The Current Page We Are On
        /// </summary>
        /// <typeparam name="T">Type Of Records That Are Returned</typeparam>
        /// <param name="CollectionToPaginate">Query To Modify</param>
        /// <param name="CurrentPageNumber">What Page Number Are You Currently On</param>
        /// <param name="HowManyRecordsPerPage">How Many Records Per Page</param>
        /// <returns>IQueryable Of T</returns>
        public static IQueryable<T> PaginateResults<T>(this IEnumerable<T> CollectionToPaginate, int CurrentPageNumber, int HowManyRecordsPerPage)
        {
            //use the main helper helper method in the regular module
            return LinqUtilities.PaginateResults(CollectionToPaginate, CurrentPageNumber, HowManyRecordsPerPage);
        }

        #endregion

    }

}
